package com.taotao.portal.controller;

import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.domain.Order;
import com.taotao.pojo.TbUser;
import com.taotao.portal.pojo.CartItem;
import com.taotao.portal.service.ICartService;
import com.taotao.portal.service.IOrderService;
import com.taotao.portal.service.IUserService;

@Controller
@RequestMapping("/order")
public class OrderController {

	@Autowired
	private ICartService cartService;

	@Autowired
	private IOrderService orderService;

	@Autowired
	private IUserService userService;

	/**
	 * 展示商品订单页面
	 * 
	 * @return
	 */
	// http://localhost:8082/order/order-cart.html
	@RequestMapping("/order-cart")
	public String showOrder(HttpServletRequest request) {
		// 获取商品类表
		List<CartItem> cartItemList = this.cartService.getCartItemList(request);
		request.setAttribute("cartList", cartItemList);
		return "order-cart";

	}

	// http://localhost:8082/order/create.html
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String createOrder(Order order, Model model, HttpServletRequest request) {
		// TaotaoResult userTaoTaoResult =
		// this.userService.getUserByToken(token);
		TbUser user = (TbUser) request.getAttribute("user");
		order.setUserId(user.getId());
		order.setBuyerNick(user.getUsername());
		order.setPostFee("0");
		TaotaoResult orderTaotaoResult = this.orderService.createOrder(order);
		if (200 == orderTaotaoResult.getStatus()) {
			// 订单号
			model.addAttribute("orderId", orderTaotaoResult.getData());
			// 付款金额
			model.addAttribute("payment", order.getPayment());
			// 抵达时间
			model.addAttribute("date", new DateTime().plusDays(4).toString("yyyy-MM-dd"));
			return "success";
		}
		return null;
	}
}
